﻿using LitJson;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TeighaHelper.Core;

namespace TeighaHelper.Demo 
{
    class Program 
    {
        [STAThread]
        static void Main(string[] args)
        {
            //string dwgPath = @"C:\___DEE\B951S-D02-116-12.DWG";
            // string dwgPath = @"J:\_workspace\_DOC_\京能康保线路路径图-塔(1).dwg";
            //string dwgPath = @"J:\XXX2010版CAD.dwg";
            //string dwgPath = @"J:\_workspace\_DOC_\架空线需求\电缆三维平台优化20220418\revit导出的文件.dwg";
            //string dbPath = @"C:\___DEE\dwg_text_sqlite.db";
            // string dbPath = @"J:\_workspace\_DOC_\1132\dwg_text_sqlite.db";
            //string dbPath =  + "\\dwg_text_sqlite.db";

            List<string> dwgPaths = FileHelper.SelectFile("dwg文件|*.dwg|所有文件|*.*", false);
            if (dwgPaths.Count() == 0) return;
            string dwgPath = dwgPaths.First();  
            string dwgDir = Path.GetDirectoryName(dwgPath);

            string jsonTextPath = dwgDir + @"\CadTextInfo.json";
            string jsonArccPath =  dwgDir + @"\CadGeoArcInfo.json";
            string jsonLinePath = dwgDir + @"\CadGeoLineInfo.json";
            string jsonPolyPath = dwgDir + @"\CadGeoPolyInfo.json";

            //SqlHelper.ClearInstance();
            //SqlHelper.GetInstanceSqlite(dbPath, true); 
            //SqliteDataAsync.InsertAsync<CADTextModel>(textModels).Result.ToList();

            try
            {
                CadDbReader.GetCadData(dwgPath);
                var dataArccs = CadDbReader.listArcs;
                var dataLines = CadDbReader.listLines;
                var dataPolys = CadDbReader.listPolylines;
                var dataDbText = CadDbReader.listDbTexts.Cast<CADTextModel>().ToList();
                var dataMText = CadDbReader.listMTexts.Cast<CADTextModel>().ToList();
                var dataTexts = dataDbText.Concat(dataMText).ToList();

                var xDatass = CadDbReader.xDatass.SelectMany(d => d).Where(x => x.Length != 0).ToList();
                 
                Console.WriteLine("arcc count :" + dataArccs.Count.ToString());
                Console.WriteLine("line count :" + dataLines.Count.ToString());
                Console.WriteLine("poly count :" + dataPolys.Count.ToString());
                Console.WriteLine("text count :" + dataTexts.Count.ToString());

                string jsonStrArcc = JsonMapper.ToJson(dataArccs);
                File.WriteAllText(jsonArccPath, jsonStrArcc, Encoding.Unicode);
                string jsonStrLine = JsonMapper.ToJson(dataLines);
                File.WriteAllText(jsonLinePath, jsonStrLine, Encoding.Unicode);
                string jsonStrPoly = JsonMapper.ToJson(dataPolys);
                File.WriteAllText(jsonPolyPath, jsonStrPoly, Encoding.Unicode);    
                string jsonStrText = JsonMapper.ToJson(dataTexts);
                File.WriteAllText(jsonTextPath, jsonStrText, Encoding.Unicode);

                string jsonStrDatass = JsonMapper.ToJson(xDatass);

                // File.WriteAllText(jsonPolyPath + "-DDPW.JSON", jsonStrDatass, Encoding.Unicode);
                //Console.WriteLine(geoTuple.Item1.Count.ToString());
                //Console.WriteLine(jsonStr.ToString());

                //string jsonStr2 = JsonMapper.ToJson(textInfo);
                //Console.WriteLine(jsonStr2.ToString());

                //------------------------------------------------------------------
                Console.WriteLine("DONE!");

            }
            catch (System.Exception exp)
            {
                Console.WriteLine(exp.ToString()); ;
            }

            Console.ReadKey();
        }
    }


    public static class FileHelper
    {
        /// <summary>
        /// 读取txt所有内容
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string TxtAllReader(string path)
        {
            string content = string.Empty;

            if (!File.Exists(path))
            { return content; }
            string str = File.ReadAllText(path);

            return str;
        }
        /// <summary>
        /// 写入txt
        /// </summary>
        /// <param name="path"></param>
        /// <param name="str"></param>
        public static void TxtWriter(string path, string str)
        {
            try
            {

                if (!File.Exists(path))
                {
                    FileStream fs1 = new FileStream(path, FileMode.Create, FileAccess.Write);
                    StreamWriter sw = new StreamWriter(fs1);
                    sw.WriteLine("\n");
                    sw.Close();
                    fs1.Close();
                }

                FileStream fs = new FileStream(path, FileMode.Append);
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.WriteLine(str + "\n");
                }
                fs.Close();


            }
            catch (Exception)
            {
                ;
            }



        }

        /// <summary>
        /// 对话框选择文件
        /// </summary>
        /// <param name="fileFilter">excel文件|*.xls|所有文件|*.*</param>
        /// <param name="multiSelect">是否多选</param>
        /// <returns></returns>
        public static List<string> SelectFile(string fileFilter, bool multiSelect)
        {
            List<string> pPaths = new List<string>();

            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = fileFilter;// "excel文件|*.xls|所有文件|*.*"; 
            openFileDialog.RestoreDirectory = false;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                openFileDialog.InitialDirectory = Path.GetDirectoryName(openFileDialog.FileNames[0]);
                if (!multiSelect) //单选
                {
                    string pPath = openFileDialog.FileName.ToString();//获取用户选择的文件路径
                    pPaths.Add(pPath);
                    return pPaths;
                }

                else if (multiSelect) //多选 
                {
                    foreach (var pPath in openFileDialog.FileNames)  //获取用户选择的文件路径
                    { pPaths.Add(pPath); }
                    return pPaths;
                }
                else { return null; }
            }
            else
            { return null; }
        }

        /// <summary>
        ///  保存对话框
        /// </summary>
        /// <param name="fName">文件全称</param>
        /// <returns></returns>
        public static string SaveFile(string fName, string postfix)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.FileName = fName; // default file name
            fileDialog.DefaultExt = postfix; // default file extension

            // bool dialogResult = fileDialog.ShowDialog();
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                string filename = fileDialog.FileName;
                string directory = Path.GetDirectoryName(filename) + "\\";
                return filename;
            }
            else
            {
                return string.Empty;
            }
        }

    }

}
